package org.example.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.auth.service.SysUserService;
import org.example.model.system.SysRole;
import org.example.model.system.SysUser;
import org.example.result.Result;
import org.example.uitls.MD5;
import org.example.vo.system.SysUserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author example
 * @since 2023-04-13
 */
@Api(tags="用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
@Autowired
    private SysUserService service;

@ApiOperation(value = "更新状态")
@GetMapping("updateStatus/{id}/{status}")
public Result updateStatus(@PathVariable Long id,
                           @PathVariable Integer status){
 service.updateStatus(id,status);
    return Result.ok();
}


    //    http://localhost:8800/doc.html
    //用户条件分页
    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo
                        ){
//        创建page对象
        Page<SysUser> pagePram=new Page<>(page,limit);
//        封装条件 判断条件值不为空
        LambdaQueryWrapper<SysUser> wrapper=new LambdaQueryWrapper<>();
//        获取条件值
        String userName=sysUserQueryVo.getKeyword();
        String createTimeBegine=sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd=sysUserQueryVo.getCreateTimeEnd();
        if (!StringUtils.isEmpty(userName)){
//            like、代表模糊查询
            wrapper.like(SysUser::getUsername,userName);
        }
        if (!StringUtils.isEmpty(createTimeBegine)){
//            大于等于
            wrapper.ge(SysUser::getCreateTime,createTimeBegine);
        }
        if (!StringUtils.isEmpty(createTimeEnd)){
//            小于等于
            wrapper.le(SysUser::getCreateTime,createTimeEnd);
        }
     IPage<SysUser>  pageModel1=service.page(pagePram,wrapper);
        return Result.ok(pageModel1);
    }
//
    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public  Result get(@PathVariable long id ){
        SysUser user= service.getById(id);
        return Result.ok(user);
    }

    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser sysUser){
//    对密码加密
        String password=sysUser.getPassword();
         String passwordMD5=MD5.encrypt(password);
         sysUser.setPassword(passwordMD5);

         service.save(sysUser);
         return Result.ok();
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public  Result update(@RequestBody SysUser sysUser){
            service.updateById(sysUser);
            return Result.ok();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result delete(@PathVariable Long id){
        service.removeById(id);
        return Result.ok();
    }

}

